<?php
class Place {
	function get_places_by_region_id($region_id) {
		include("inc_/sup3rs3cr3tt3xt4m3t0kn0vv.php");
		$mysqli = new mysqli($host, $user, $password, $database);
		if(mysqli_connect_errno()) {
			printf("Connect failed: %s\n", mysqli_connect_error());
			exit();
		}
		$sql = "SELECT place_id, place_name FROM places WHERE region_id = '$region_id'";
		if($result = $mysqli->query($sql)) {
			if($result->num_rows >= 1) {
				while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
					extract($row);
					$each = array(
						"id" => "$place_id",
						"name" => "$place_name",
					);
					$option_list[] = $each;
				}
				return $option_list;
			}
			$result->close();
		}
		$mysqli->close();
	}
	
	function get_place_lat_long($place_id) {
		include("inc_/sup3rs3cr3tt3xt4m3t0kn0vv.php");
		$mysqli = new mysqli($host, $user, $password, $database);
		if(mysqli_connect_errno()) {
			printf("Connect failed: %s\n", mysqli_connect_error());
			exit();
		}
		$sql = "SELECT place_lat, place_long FROM places WHERE place_id = '$place_id'";
		if($result = $mysqli->query($sql)) {
			if($result->num_rows >= 1) {
				while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
					extract($row);
					$each = array(
						"lat" => "$place_lat",
						"long" => "$place_long",
					);
					$option_list[] = $each;
				}
				return $option_list;
			}
			$result->close();
		}
		$mysqli->close();
	}
	
	function vote($place_id, $user_id, $new_vote) {
		include("inc_/sup3rs3cr3tt3xt4m3t0kn0vv.php");
		$mysqli = new mysqli($host, $user, $password, $database);
		if(mysqli_connect_errno()) {
			printf("Connect failed: %s\n", mysqli_connect_error());
			exit();
		}
		$sql = "SELECT vote, vote_id FROM place_vote WHERE place_id = '$place_id' AND user_id ='$user_id'";
		if($result = $mysqli->query($sql)) {
			if($result->num_rows >= 1) {
				while($row = $result->fetch_array(MYSQLI_ASSOC)) {
					extract($row);
					if($vote == $new_vote) {
						return 0;
					} else if($vote != $new_vote) {
						$sql = "DELETE FROM place_vote WHERE vote_id = '$vote_id'";
						$mysqli->query($sql);
						$sql = "INSERT INTO place_vote(place_id, user_id, vote) VALUES('$place_id', '$user_id', '$new_vote')";
						$mysqli->query($sql);
						return 3;
					}
				}
			} else {
				$sql = "INSERT INTO place_vote(place_id, user_id, vote) VALUES('$place_id', '$user_id', '$new_vote')";
				$mysqli->query($sql);
				return 1;
			}
			$result->close();
		} else {
			return 4;
		}
		$mysqli->close();
	}
	
	function get_vote($place_id) {
		include("inc_/sup3rs3cr3tt3xt4m3t0kn0vv.php");
		$mysqli = new mysqli($host, $user, $password, $database);
		if(mysqli_connect_errno()) {
			printf("Connect failed: %s\n", mysqli_connect_error());
			exit();
		}
		$sql = "SELECT sum(vote) AS votes FROM place_vote WHERE place_id ='$place_id'";
		if($result = $mysqli->query($sql)) {
			if($result->num_rows >= 1) {
				while($row = $result->fetch_array(MYSQLI_ASSOC)) {
					extract($row);
					return $votes;
				}
			} else {
				$votes = '0';
				return $votes;
			}
		}
		$result->close();
		$mysqli->close();
	}
	
	function add_place($region, $lat, $long, $name) {
		include("inc_/sup3rs3cr3tt3xt4m3t0kn0vv.php");
		$mysqli = new mysqli($host, $user, $password, $database);
		if(mysqli_connect_errno()) {
			printf("Connect failed: %s\n", mysqli_connect_error());
			exit();
		}
		$sql = "SELECT place_id FROM places WHERE place_name = '$name' AND region_id = '$region'";
		if($result = $mysqli->query($sql)) {
			if($result->num_rows >= 1) {
				return 0;
			} else {
				$sql = "INSERT INTO places(region_id, place_name, place_lat, place_long) VALUES('$region', '$name', '$lat', '$long')";
				$mysqli->query($sql);
				$inserted = $mysqli->insert_id;
				return $inserted;
			}
		}
	}
	
	function get_all_place_photos($place_id) {
		include("inc_/sup3rs3cr3tt3xt4m3t0kn0vv.php");
		$mysqli = new mysqli($host, $user, $password, $database);
		if(mysqli_connect_errno()) {
			printf("Connect failed: %s\n", mysqli_connect_error());
			exit();
		}
		$sql = "SELECT photo_file FROM place_photo WHERE place_id='$place_id'";
		if($result = $mysqli->query($sql)) {
			if($result->num_rows >= 1) {
				while($row = $result->fetch_array(MYSQLI_ASSOC)) {
					extract($row);
					$array[] = $photo_file;
				}
				return $array;
			} else {
				return 0;
			}
		}
	}
}
?>